package com.ruyuan2020.im.code.l618;



import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class DelayedTask implements Delayed {
    private final long delayTime;
    private final long expireTime;
    private final String taskName;

    public DelayedTask(long delayTime, String taskName) {
        this.delayTime = delayTime;
        this.taskName = taskName;
        this.expireTime = System.currentTimeMillis() + delayTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long diff = expireTime - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if (this.expireTime < ((DelayedTask) o).expireTime) {
            return -1;
        } else if (this.expireTime > ((DelayedTask) o).expireTime) {
            return 1;
        }
        return 0;
    }

    public String getTaskName() {
        return taskName;
    }
}

